package MainPackage;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import framework.Translator;

import JassTranslator.ImportJass;

public class TranslateFileController {
	static String inputStringFile = "c:\\TestFiles\\tester.j";
	static String outputStringFile = "";
	static Class translatorClass = ImportJass.class;

	File inputFile;
	File outputFile;

	BufferedReader inputStream;
	BufferedWriter outputStream;

	public BufferedReader getBufferedReader(String file)
			throws FileNotFoundException {
		inputFile = new File(file);
		return new BufferedReader(new FileReader(inputFile));
	}

	private BufferedWriter getBufferedWriter(String jassFile)
			throws IOException {
		outputFile = new File(jassFile);
		return new BufferedWriter(new FileWriter(outputFile));
	}

	public File directlyTranslateFile(String inputFile) throws Exception {
		int counter = 1;
		try {
			Translator translator = (Translator) translatorClass.newInstance();
			String outputFile = inputFile.substring(0, inputFile.indexOf("."))
					+ translator.getExportFileExtension();

			inputStream = getBufferedReader(inputFile);
			outputStream = getBufferedWriter(outputFile);

			String thisLine;
			while ((thisLine = inputStream.readLine()) != null) {
				List<String> codeLines = translator.translateLine(thisLine);
				for (String line : codeLines) {
					outputStream.append(line);
					outputStream.newLine();
				}
				counter++;
			}
		} catch (Exception e) {
			System.out.println("Exception on line " + counter);
			throw e;
		} finally {
			outputStream.close();
		}

		return outputFile;
	}

	public static void main(String... args) throws Exception {
		new TranslateFileController().directlyTranslateFile(inputStringFile);
	}
}
